package com.exercise.core.util;/**
 * Created by kete on 2017-05-05.
 */

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 作者：kete
 * 创建时间：2017-05-05 15:34
 * 功能描述：
 * 版本：
 */
public class WebUtil {

    private static Logger logger = LoggerFactory.getLogger(WebUtil.class);
    public static String accessSecret = "imQIWVZiX8qTo8hzFyCiQfKEOKygNZ5bQMC7BmusGbJ";
    public static String nodeName = null;
    public static String webdocsURL = null;
    public static boolean isWebdocs = false;
    public static String configPrefix = null;


    private static Object lockContext = new Object();

    /**
     * 作者：闫广坤
     * 创建时间：2014-12-09 下午1:48:19
     * 方法描述：获取request。
     * @return
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    public static HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();

        return response;
    }

    public static HttpSession getSession() {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession();
    }


    public static String getContextPath() {
        return getRequest().getContextPath();
    }





    public static RequestContext getRequestContext() {

        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        RequestContext requestContext = new RequestContext(request);
        return requestContext;
    }

    public static String getURI(){
        return getRequest().getRequestURI();
    }

    public static String[] domains = new String[] {"onelinkplus.com","fosun.com"};

    public static String getDomain() {
        HttpServletRequest request = WebUtil.getRequest();
        String host = request.getHeader("Host");
        if(StringUtils.isEmpty(host)) return null;

        for(String domain:domains) {
            if(host.indexOf(domain) != -1) {
                return domain;
            }
        }

        return null;
    }

    public static boolean isFosunDoamin() {
        String doamin = getDomain();
        if(StringUtils.equalsIgnoreCase("fosun.com",doamin)) {
            return true;
        }
        return false;
    }

    public static void cookieSetDomain(Cookie cookie) {

        String domain = getDomain();
        if(StringUtils.isNotEmpty(domain)) {
            cookie.setDomain("." + domain);
        }
    }
}
